package com.pzsh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.pzsh.aliyun.api.gateway.demo.util.HttpUtils;
import com.pzsh.constant.EOLINKERConstants;
import com.pzsh.constant.ThreadPoolsConstants;
import com.pzsh.entity.*;
import com.pzsh.entity.vo.*;
import com.pzsh.entity.vo.admin.AdminCensusVo;
import com.pzsh.entity.vo.admin.AdminGoodsVo;
import com.pzsh.entity.vo.admin.AdminGradeVo;
import com.pzsh.enums.ErrorCodeEnum;
import com.pzsh.exception.ApiException;
import com.pzsh.mapper.*;
import com.pzsh.service.IGoodsService;
import com.pzsh.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.RowBounds;
import org.bouncycastle.asn1.cryptopro.Gost2814789EncryptedKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMethod;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description:
 * @Param:
 * @return:
 * @Author: GXL
 * @Date: 2019/12/06
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS)
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GradeMapper gradeMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ReceivAddressMapper receivAddressMapper;

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private GoodsRotationMapper goodsRotationMapper;

    @Autowired
    private UserPackMapper userPackMapper;
    @Autowired
    private RewardTaskMapper rewardTaskMapper;

    @Autowired
    private SpendMoneyMapper spendMoneyMapper;

    @Autowired
    private FriendsMapper friendsMapper;

    @Autowired
    private CensusMapper censusMapper;

    @Autowired
    private UserProfitMapper userProfitMapper;

    @Autowired
    private UserProfitTodayMapper userProfitTodayMapper;

    @Autowired
    private TransactionssMapper transactionssMapper;

    @Autowired
    private OperatorLogsMapper operatorLogsMapper;

    @Autowired
    private SpecialMapper specialMapper;

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public List<GradeVo> findGradeList() {
        List<GradeEntity> list = gradeMapper.selectAll();

        List<GradeVo> returnList = new ArrayList<>();
        for (GradeEntity entity : list) {

            BigDecimal price = new BigDecimal(entity.getPrice().toString());
            BigDecimal disPrice = new BigDecimal(entity.getDisPrice().toString());
            BigDecimal givePs = new BigDecimal(entity.getGivePs().toString());
            BigDecimal oneh = new BigDecimal(100);

            GradeVo gradeVo = new GradeVo();
            gradeVo.setDisPrice(String.valueOf(disPrice.divide(oneh, 2, RoundingMode.HALF_UP)));
            gradeVo.setGid(entity.getGid());
            gradeVo.setGivePs(String.valueOf(givePs.divide(oneh, 2, RoundingMode.HALF_UP)));
            gradeVo.setGradeId(entity.getGradeId());
            gradeVo.setGradeName(entity.getGradeName());
            gradeVo.setMainPicture(entity.getMainPicture());
            gradeVo.setPrice(String.valueOf(price.divide(oneh, 2, RoundingMode.HALF_UP)));
            returnList.add(gradeVo);
        }
        return returnList;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public List<GoodsVo> findGoodsList() {
        List<GoodsEntity> list = goodsMapper.selectAll();

        List<GoodsVo> returnList = new ArrayList<>();
        for (GoodsEntity entity : list) {

            BigDecimal price = new BigDecimal(entity.getPrice().toString());
            BigDecimal disPrice = new BigDecimal(entity.getDisPrice().toString());
            BigDecimal rewardPs = new BigDecimal(entity.getRewardPs().toString());
            BigDecimal oneh = new BigDecimal(100);

            GoodsVo goodsVo = new GoodsVo();
            goodsVo.setDisPrice(String.valueOf(disPrice.divide(oneh, 2, RoundingMode.HALF_UP)));
            goodsVo.setGid(entity.getGid());
            goodsVo.setGoodsId(entity.getGoodsId());
            goodsVo.setMainPicture(entity.getMainPicture());
            goodsVo.setPrice(String.valueOf(price.divide(oneh, 2, RoundingMode.HALF_UP)));
            goodsVo.setGoodsName(entity.getGoodsName());
            goodsVo.setRewardPs(String.valueOf(rewardPs.divide(oneh, 2, RoundingMode.HALF_UP)));
            returnList.add(goodsVo);
        }
        return returnList;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public GoodsVo getGoodsInfo(Integer goodsId) {
        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity.setGoodsId(goodsId);
        goodsEntity = goodsMapper.selectOne(goodsEntity);

        BigDecimal price = new BigDecimal(goodsEntity.getPrice().toString());
        BigDecimal disPrice = new BigDecimal(goodsEntity.getDisPrice().toString());
        BigDecimal rewardPs = new BigDecimal(goodsEntity.getRewardPs().toString());
        BigDecimal oneh = new BigDecimal(100);


        GoodsVo goodsVo = new GoodsVo();
        goodsVo.setRewardPs(String.valueOf(rewardPs.divide(oneh, 2, RoundingMode.HALF_UP)));
        goodsVo.setGoodsName(goodsEntity.getGoodsName());
        goodsVo.setDisPrice(String.valueOf(disPrice.divide(oneh, 2, RoundingMode.HALF_UP)));
        goodsVo.setGid(goodsEntity.getGid());
        goodsVo.setGoodsId(goodsEntity.getGoodsId());
        goodsVo.setMainPicture(goodsEntity.getMainPicture());
        goodsVo.setPrice(String.valueOf(price.divide(oneh, 2, RoundingMode.HALF_UP)));
        goodsVo.setContent(goodsEntity.getContent());

        //获取商品轮播图
        GoodsRotationEntity obj = new GoodsRotationEntity();
        obj.setGoodsId(goodsId);
        obj.setGType(2);
        List<GoodsRotationEntity> list = goodsRotationMapper.select(obj);
        List<GoodsRotaionVo> vos = new ArrayList<>();
        for (GoodsRotationEntity entity : list) {
            GoodsRotaionVo vo = new GoodsRotaionVo();
            vo.setGoodsId(entity.getGoodsId());
            vo.setId(entity.getId());
            vo.setPicSort(entity.getPicSort());
            vo.setPicUrl(entity.getPicUrl());
            vos.add(vo);
        }
        goodsVo.setGoodsRotaionVoList(vos);

        return goodsVo;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public GradeVo getGradeInfo(Integer gradeId) {

        GradeEntity gradeEntity = new GradeEntity();
        gradeEntity.setGradeId(gradeId);
        gradeEntity = gradeMapper.selectOne(gradeEntity);

        BigDecimal price = new BigDecimal(gradeEntity.getPrice().toString());
        BigDecimal disPrice = new BigDecimal(gradeEntity.getDisPrice().toString());
        BigDecimal givePs = new BigDecimal(gradeEntity.getGivePs().toString());
        BigDecimal oneh = new BigDecimal(100);

        GradeVo gradeVo = new GradeVo();
        gradeVo.setPrice(String.valueOf(price.divide(oneh, 2, RoundingMode.HALF_UP)));
        gradeVo.setMainPicture(gradeEntity.getMainPicture());
        gradeVo.setGradeName(gradeEntity.getGradeName());
        gradeVo.setGradeId(gradeEntity.getGradeId());
        gradeVo.setGivePs(String.valueOf(givePs.divide(oneh, 2, RoundingMode.HALF_UP)));
        gradeVo.setGid(gradeEntity.getGid());
        gradeVo.setDisPrice(String.valueOf(disPrice.divide(oneh, 2, RoundingMode.HALF_UP)));
        gradeVo.setContent(gradeEntity.getContent());

        //获取商品轮播图
        GoodsRotationEntity obj = new GoodsRotationEntity();
        obj.setGoodsId(gradeId);
        obj.setGType(1);
        List<GoodsRotationEntity> list = goodsRotationMapper.select(obj);
        List<GoodsRotaionVo> vos = new ArrayList<>();
        for (GoodsRotationEntity entity : list) {
            GoodsRotaionVo vo = new GoodsRotaionVo();
            vo.setGoodsId(entity.getGoodsId());
            vo.setId(entity.getId());
            vo.setPicSort(entity.getPicSort());
            vo.setPicUrl(entity.getPicUrl());
            vos.add(vo);
        }
        gradeVo.setGoodsRotaionVoList(vos);

        return gradeVo;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void buyGrade(String account, Integer gradeId, Integer numb, String receiver, String mobile, String city, String address) {

        GradeEntity gradeEntity = new GradeEntity();
        gradeEntity.setGradeId(gradeId);
        gradeEntity = gradeMapper.selectOne(gradeEntity);

        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(account);
        userEntity = userMapper.selectOne(userEntity);

        if (!userEntity.getGrade().equals(gradeEntity.getFromGrade())){
            throw new ApiException(ErrorCodeEnum.PARAM_200007.code(),ErrorCodeEnum.PARAM_200007.msg());
        }

        //判断直推人数是否符合购买条件
//        FriendsEntity friendsEntity = new FriendsEntity();
//        friendsEntity.setUserId(userEntity.getUserId());
//        int count = friendsMapper.selectCount(friendsEntity);
//        if (count < gradeEntity.getRecommend()){
//            throw new ApiException(ErrorCodeEnum.PARAM_200018.code(),ErrorCodeEnum.PARAM_200018.msg());
//        }

        //判断团队人数--数据来源RD系统
        Integer group = 0 ;
        //判断直推人数是否符合购买条件
        Integer child = 0 ;
        try {
            Map<String, String> headers = new HashMap<String, String>();
            Map<String, String> querys = new HashMap<String, String>();
            Map<String, String> bodys = new HashMap<String, String>();
            bodys.put("username", userEntity.getAccount());
            HttpResponse response = HttpUtils.doPost(EOLINKERConstants.DOMAIN_URL,EOLINKERConstants.USER_NUMBER, RequestMethod.POST.name(),headers,querys,bodys);
            String str = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSONObject.parseObject(str);
            log.info("RD系统返回-我的会员人数统计：{}",jsonObject.toJSONString());
            Integer status = jsonObject.getInteger("status");
            if (!status.equals(0)){
                throw new ApiException(ErrorCodeEnum.PARAM_200014.code(),String.format(ErrorCodeEnum.PARAM_200014.msg(),"我的会员人数统计"));
            }
            group = jsonObject.getJSONObject("data").getInteger("group");
            child = jsonObject.getJSONObject("data").getInteger("child");
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            throw new ApiException(ErrorCodeEnum.PARAM_200014.code(),String.format(ErrorCodeEnum.PARAM_200014.msg(),"我的会员人数统计"));
        }
        if (child == null || child < gradeEntity.getRecommend()){
            throw new ApiException(ErrorCodeEnum.PARAM_200018.code(),ErrorCodeEnum.PARAM_200018.msg());
        }
        if (group == null || group < gradeEntity.getTeam()){
            throw new ApiException(ErrorCodeEnum.PARAM_200019.code(),ErrorCodeEnum.PARAM_200019.msg());
        }

        //用户自由积分，赠送积分更新
        UserPackEntity userPackEntity = new UserPackEntity();
        userPackEntity.setUserId(userEntity.getUserId());
        userPackEntity = userPackMapper.selectOne(userPackEntity);
        if(userPackEntity.getFreeIntegral() <= 0 || userPackEntity.getFreeIntegral() < (gradeEntity.getDisPrice() * numb)){
            throw new ApiException(ErrorCodeEnum.SW302.code(),ErrorCodeEnum.SW302.msg());
        }
        Integer integral = userPackEntity.getFreeIntegral() - (gradeEntity.getDisPrice() * numb);//消费扣掉自由积分, 使用优惠价
        Integer giveIntegral = gradeEntity.getGivePs();//赠送积分，只能用于消费
        userPackEntity.setFreeIntegral(integral);
        userPackEntity.setRewardIntegral(userPackEntity.getRewardIntegral() + giveIntegral);
        userPackMapper.updateByPrimaryKey(userPackEntity);

        //用户等级更新 (交由RD系统处理后通知)
//        userEntity.setGrade(gradeEntity.getToGrade());
//        userMapper.updateByPrimaryKey(userEntity);

        //更新累计消费
        UserSpendEntity userSpendEntity = new UserSpendEntity();
        userSpendEntity.setUserId(userEntity.getUserId());
        userSpendEntity = spendMoneyMapper.selectOne(userSpendEntity);
        userSpendEntity.setSpendMoneyGrade(userSpendEntity.getSpendMoneyGrade() + gradeEntity.getDisPrice());
        userSpendEntity.setSpendMoney(userSpendEntity.getSpendMoney() + gradeEntity.getDisPrice());
        spendMoneyMapper.updateByPrimaryKey(userSpendEntity);

        //保存收货地址
        ReceivAddressEntity receivAddressEntity = new ReceivAddressEntity();
        receivAddressEntity.setUserId(userEntity.getUserId());
        receivAddressEntity = receivAddressMapper.selectOne(receivAddressEntity);
        if (receivAddressEntity == null){
            receivAddressEntity = new ReceivAddressEntity();
            receivAddressEntity.setUserId(userEntity.getUserId());
            receivAddressEntity.setCityProv(city);
            receivAddressEntity.setAddress(address);
            receivAddressEntity.setConsigneeMobile(mobile);
            receivAddressEntity.setConsignee(receiver);
            receivAddressMapper.insert(receivAddressEntity);
        }

        //保存订单记录
        String uuid = UUID.randomUUID().toString().replace("-","");
        OrderEntity entity = new OrderEntity();
        entity.setGType(2);
        entity.setStatus(3);//升级套餐，购买后默认为：确认收货
        entity.setGid(gradeEntity.getGid());
        entity.setGoodsName(gradeEntity.getGradeName());
        entity.setDisPrice(gradeEntity.getDisPrice());
        entity.setMainPicture(gradeEntity.getMainPicture());
        entity.setSerialNum(uuid);//订单流水号
        entity.setPrice(gradeEntity.getPrice());
        entity.setGivePs(gradeEntity.getGivePs());
        entity.setTotalPrice(gradeEntity.getDisPrice());
        entity.setUserId(userEntity.getUserId());
        entity.setToGrade(gradeEntity.getToGrade());
        entity.setFromGrade(gradeEntity.getFromGrade());
        entity.setTeam(gradeEntity.getTeam());
        entity.setReConsumption(gradeEntity.getReConsumption());
        entity.setRecommend(gradeEntity.getRecommend());
        entity.setCreateDate(new Timestamp(new Date().getTime()));
        entity.setModifyDate(new Timestamp(new Date().getTime()));
        entity.setAddress(address);
        entity.setConsignee(receiver);
        entity.setConsigneeMobile(mobile);
        entity.setCityProv(city);
        entity.setAccount(userEntity.getAccount());
        orderMapper.insert(entity);

        //添加明细记录 用户购买升级套餐赠送消费积分入账记录
        TransactionEntity in = new TransactionEntity();
        in.setNumb(gradeEntity.getGivePs());
        in.setRemark("赠送消费积分");
        in.setTType(8);
        in.setCreateDate(new Timestamp(new Date().getTime()));
        in.setModifyDate(new Timestamp(new Date().getTime()));
        in.setRecordType(0);
        in.setInoutType(1);
        in.setUserId(userEntity.getUserId());
        in.setAccount(userEntity.getAccount());
        transactionssMapper.insert(in);

        //添加明细记录 用户购买升级套餐出账记录
        TransactionEntity out = new TransactionEntity();
        out.setNumb(gradeEntity.getDisPrice());
        out.setRemark("购买升级套餐");
        out.setTType(11);
        out.setCreateDate(new Timestamp(new Date().getTime()));
        out.setModifyDate(new Timestamp(new Date().getTime()));
        out.setRecordType(0);
        out.setInoutType(0);
        out.setUserId(userEntity.getUserId());
        out.setAccount(userEntity.getAccount());
        transactionssMapper.insert(out);

        //**累计升级套餐总消费ps
        CensusEntity censusEntity = censusMapper.selectAll().get(0);
        censusEntity.setCountGradeOrder(censusEntity.getCountGradeOrder() + 1);
        censusEntity.setCountGradeBuy(censusEntity.getCountGradeBuy() + gradeEntity.getDisPrice());
        censusEntity.setCountGivePs(censusEntity.getCountGivePs() + gradeEntity.getGivePs());
        censusMapper.updateByPrimaryKey(censusEntity);

        //累计赠送PS收益
        UserProfitEntity userProfitEntity = new UserProfitEntity();
        userProfitEntity.setUserId(userEntity.getUserId());
        userProfitEntity = userProfitMapper.selectOne(userProfitEntity);
        userProfitEntity.setPsProfit(userProfitEntity.getPsProfit() + gradeEntity.getGivePs());
        int ddd = userProfitMapper.updateByPrimaryKey(userProfitEntity);
        log.info("累计赠送用户ps收益：{},更新：{}",gradeEntity.getGivePs(),ddd);

        //累计赠送用户今日PS收益
        Long nowTime = new Date().getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        UserProfitTodayEntity userProfitTodayEntity = new UserProfitTodayEntity();
        userProfitTodayEntity.setUserId(userEntity.getUserId());
        userProfitTodayEntity.setToday(dateFormat.format(nowTime));
        userProfitTodayEntity = userProfitTodayMapper.selectOne(userProfitTodayEntity);
        userProfitTodayEntity.setPsProfit(userProfitTodayEntity.getPsProfit() + gradeEntity.getGivePs());
        int dd = userProfitTodayMapper.updateByPrimaryKey(userProfitTodayEntity);
        log.info("累计赠送用户今日PS收益：{},更新：{}",gradeEntity.getGivePs(),dd);
        //请求RD积分系统接口--创建订单
        try {
            Map<String, String> headers = new HashMap<String, String>();
            Map<String, String> querys = new HashMap<String, String>();
            Map<String, String> bodys = new HashMap<String, String>();
            bodys.put("no", entity.getSerialNum());
            bodys.put("username", userEntity.getAccount());
            Double disPrice = Double.valueOf(gradeEntity.getDisPrice() / 100);
            bodys.put("money", disPrice.toString());
            bodys.put("type", String.valueOf(gradeEntity.getToGrade() + 1));
            bodys.put("wallet", "1");
            HttpResponse response = HttpUtils.doPost(EOLINKERConstants.DOMAIN_URL,EOLINKERConstants.ORDER_ADD, RequestMethod.POST.name(),headers,querys,bodys);
            String str = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSONObject.parseObject(str);
            log.info("RD系统返回-创建订单(升级套餐)：{}",jsonObject.toJSONString());
            Integer status = jsonObject.getInteger("status");
            if (!status.equals(0)){
                throw new ApiException(ErrorCodeEnum.PARAM_200014.code(),String.format(ErrorCodeEnum.PARAM_200014.msg(),"创建订单(升级套餐)"));
            }
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            throw new ApiException(ErrorCodeEnum.PARAM_200014.code(),String.format(ErrorCodeEnum.PARAM_200014.msg(),"创建订单(升级套餐)"));
        }

    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void buyGoods(String account, Integer goodsId, Integer numb, String receiver, String mobile, String city, String address) {
        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity.setGoodsId(goodsId);
        goodsEntity = goodsMapper.selectOne(goodsEntity);

        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(account);
        userEntity = userMapper.selectOne(userEntity);

        ReceivAddressEntity receivAddressEntity = new ReceivAddressEntity();
        receivAddressEntity.setUserId(userEntity.getUserId());
        receivAddressEntity = receivAddressMapper.selectOne(receivAddressEntity);
        if (receivAddressEntity == null){
            receivAddressEntity = new ReceivAddressEntity();
            receivAddressEntity.setUserId(userEntity.getUserId());
            receivAddressEntity.setCityProv(city);
            receivAddressEntity.setAddress(address);
            receivAddressEntity.setConsigneeMobile(mobile);
            receivAddressEntity.setConsignee(receiver);
            receivAddressMapper.insert(receivAddressEntity);
        }

        //用户赠送积分更新
        UserPackEntity userPackEntity = new UserPackEntity();
        userPackEntity.setUserId(userEntity.getUserId());
        userPackEntity = userPackMapper.selectOne(userPackEntity);

        Integer kfee = userPackEntity.getFreeIntegral() + userPackEntity.getRewardIntegral();
        Integer totalFee = goodsEntity.getDisPrice() * numb;
        Integer totalReward = goodsEntity.getRewardPs() * numb;
        if(kfee <= 0 || kfee < totalFee){
            throw new ApiException(ErrorCodeEnum.SW302.code(),ErrorCodeEnum.SW302.msg());
        }
        Integer integral = userPackEntity.getRewardIntegral() - totalFee;

        if (integral < 0){ //如果消费积分不足以全部抵扣订单总价，则按自由积分占总支付比例算出该赠送多少消费积分
            BigDecimal D1 = new BigDecimal(Math.abs(integral));
            BigDecimal D2 = new BigDecimal(totalFee);
            BigDecimal D3 = D1.divide(D2,2,RoundingMode.HALF_UP);
            BigDecimal D4 = new BigDecimal(totalReward);
            BigDecimal D5 = D4.multiply(D3);
            totalReward = D5.intValue();
        }else{
            totalReward = 0;
        }


        //用于订单流水号
        String uuid = UUID.randomUUID().toString().replace("-","");
        //保存订单记录
        OrderEntity entity = new OrderEntity();
        entity.setGType(1);
        entity.setStatus(1);//默认为未支付
        entity.setGid(goodsEntity.getGid());
        entity.setRewardPs(totalReward);
//        entity.setRewardPs(goodsEntity.getRewardPs());//未计算实际奖励，用户确认收货后会更新此值
        entity.setGoodsName(goodsEntity.getGoodsName());
        entity.setDisPrice(goodsEntity.getDisPrice());
        entity.setMainPicture(goodsEntity.getMainPicture());
        entity.setNumb(numb);
        entity.setSerialNum(uuid);//订单流水号
        entity.setPrice(goodsEntity.getPrice());
//        entity.setTotalPrice(totalFee);
        entity.setTotalPrice(goodsEntity.getDisPrice() * numb);
        entity.setUserId(userEntity.getUserId());
        entity.setReturnDays(goodsEntity.getReturnDays());
        entity.setCreateDate(new Timestamp(new Date().getTime()));
        entity.setModifyDate(new Timestamp(new Date().getTime()));
        entity.setAddress(address);
        entity.setConsignee(receiver);
        entity.setConsigneeMobile(mobile);
        entity.setCityProv(city);
        entity.setAccount(userEntity.getAccount());
        orderMapper.insert(entity);

        if (integral >= 0){
            userPackEntity.setRewardIntegral(userPackEntity.getRewardIntegral() - totalFee);//消费扣掉赠送积分, 使用优惠价

            //添加明细记录 用户购买商城商品出账记录
            TransactionEntity out = new TransactionEntity();
            out.setNumb(totalFee);
            out.setRemark("消费积分购买商城商品");
            out.setTType(12);
            out.setCreateDate(new Timestamp(new Date().getTime()));
            out.setModifyDate(new Timestamp(new Date().getTime()));
            out.setRecordType(0);
            out.setInoutType(0);
            out.setUserId(userEntity.getUserId());
            out.setAccount(userEntity.getAccount());
            transactionssMapper.insert(out);
            //添加日志记录(用于取消订单时抵消财务记录)
            OperatorLogsEntity operatorLogsEntity = new OperatorLogsEntity();
            operatorLogsEntity.setUserId(userEntity.getUserId());
            operatorLogsEntity.setTranId(out.getId());
            operatorLogsEntity.setOrderId(entity.getOrderId());
            operatorLogsEntity.setNumb(totalFee);
            operatorLogsEntity.setPackType(1);
            operatorLogsEntity.setCreateDate(new Timestamp(new Date().getTime()));
            operatorLogsEntity.setModifyDate(new Timestamp(new Date().getTime()));
            operatorLogsMapper.insert(operatorLogsEntity);

        }else {
            if (userPackEntity.getRewardIntegral() > 0){
                //添加明细记录 用户购买商城商品出账记录
                TransactionEntity out = new TransactionEntity();
                out.setNumb(userPackEntity.getRewardIntegral());
                out.setRemark("消费积分购买商城商品");
                out.setTType(12);
                out.setCreateDate(new Timestamp(new Date().getTime()));
                out.setModifyDate(new Timestamp(new Date().getTime()));
                out.setRecordType(0);
                out.setInoutType(0);
                out.setUserId(userEntity.getUserId());
                out.setAccount(userEntity.getAccount());
                transactionssMapper.insert(out);
                //添加日志记录(用于取消订单时抵消财务记录)
                OperatorLogsEntity operatorLogsEntity = new OperatorLogsEntity();
                operatorLogsEntity.setUserId(userEntity.getUserId());
                operatorLogsEntity.setTranId(out.getId());
                operatorLogsEntity.setOrderId(entity.getOrderId());
                operatorLogsEntity.setNumb(userPackEntity.getRewardIntegral());
                operatorLogsEntity.setPackType(1);
                operatorLogsEntity.setCreateDate(new Timestamp(new Date().getTime()));
                operatorLogsEntity.setModifyDate(new Timestamp(new Date().getTime()));
                operatorLogsMapper.insert(operatorLogsEntity);

                userPackEntity.setRewardIntegral(0);
            }
            userPackEntity.setFreeIntegral(userPackEntity.getFreeIntegral() + integral);//消费积分不足扣自由积分
            //添加明细记录 用户购买商城商品出账记录
            TransactionEntity out = new TransactionEntity();
            out.setNumb(Math.abs(integral));
            out.setRemark("PS购买商城商品");
            out.setTType(12);
            out.setCreateDate(new Timestamp(new Date().getTime()));
            out.setModifyDate(new Timestamp(new Date().getTime()));
            out.setRecordType(0);
            out.setInoutType(0);
            out.setUserId(userEntity.getUserId());
            out.setAccount(userEntity.getAccount());
            transactionssMapper.insert(out);
            //添加日志记录(用于取消订单时抵消财务记录)
            OperatorLogsEntity operatorLogsEntity = new OperatorLogsEntity();
            operatorLogsEntity.setUserId(userEntity.getUserId());
            operatorLogsEntity.setTranId(out.getId());
            operatorLogsEntity.setOrderId(entity.getOrderId());
            operatorLogsEntity.setNumb(Math.abs(integral));
            operatorLogsEntity.setPackType(0);
            operatorLogsEntity.setCreateDate(new Timestamp(new Date().getTime()));
            operatorLogsEntity.setModifyDate(new Timestamp(new Date().getTime()));
            operatorLogsMapper.insert(operatorLogsEntity);

        }
        userPackMapper.updateByPrimaryKey(userPackEntity);


    }
    @Transactional(propagation=Propagation.REQUIRED)
    //确认订单页，暂时不用
    @Override
    public OrderVo confirmOrderGoods(String account, Integer goodsId, Integer numb, String receiver, String mobile, String city, String address) {

        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity.setGoodsId(goodsId);
        goodsEntity = goodsMapper.selectOne(goodsEntity);

        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(account);
        userEntity = userMapper.selectOne(userEntity);

        ReceivAddressEntity receivAddressEntity = new ReceivAddressEntity();
        receivAddressEntity.setUserId(userEntity.getUserId());
        receivAddressEntity = receivAddressMapper.selectOne(receivAddressEntity);

        String uuid = UUID.randomUUID().toString().replace("-","");

        //保存订单记录
        OrderEntity entity = new OrderEntity();
        if (receivAddressEntity != null){
            entity.setAddress(receivAddressEntity.getAddress());
            entity.setConsignee(receivAddressEntity.getConsignee());
            entity.setConsigneeMobile(receivAddressEntity.getConsigneeMobile());
        }else{
            receivAddressEntity.setCityProv(city);
            receivAddressEntity.setAddress(address);
            receivAddressEntity.setConsigneeMobile(mobile);
            receivAddressEntity.setConsignee(receiver);
            receivAddressMapper.insert(receivAddressEntity);
        }
        entity.setGType(1);
        entity.setStatus(0);//默认为未支付
        entity.setGid(goodsEntity.getGid());
        entity.setRewardPs(goodsEntity.getRewardPs());
        entity.setGoodsName(goodsEntity.getGoodsName());
        entity.setDisPrice(goodsEntity.getDisPrice());
        entity.setMainPicture(goodsEntity.getMainPicture());
        entity.setNumb(numb);
        entity.setSerialNum(uuid);//订单流水号
        entity.setPrice(goodsEntity.getPrice());
        entity.setTotalPrice(goodsEntity.getPrice() * numb);
        entity.setUserId(userEntity.getUserId());
        entity.setReturnDays(goodsEntity.getReturnDays());
        entity.setCreateDate(new Timestamp(new Date().getTime()));
        entity.setModifyDate(new Timestamp(new Date().getTime()));
        entity.setAddress(address);
        entity.setConsignee(receiver);
        entity.setConsigneeMobile(mobile);
        entity.setCityProv(city);
        orderMapper.insert(entity);

        //返回订单信息
        OrderVo orderVo = new OrderVo();
        if (receivAddressEntity != null){
            orderVo.setAddress(receivAddressEntity.getAddress());
            orderVo.setConsignee(receivAddressEntity.getConsignee());
            orderVo.setConsigneeMobile(receivAddressEntity.getConsigneeMobile());
        }
        orderVo.setStatus(0);//默认为未支付
        orderVo.setGid(goodsEntity.getGid());
//        orderVo.setRewardPs(goodsEntity.getRewardPs());
//        orderVo.setPrice(goodsEntity.getPrice());
//        orderVo.setTotalPrice(goodsEntity.getPrice() * numb);
//        orderVo.setDisPrice(goodsEntity.getDisPrice());
        orderVo.setGoodsName(goodsEntity.getGoodsName());
        orderVo.setMainPicture(goodsEntity.getMainPicture());
        orderVo.setNumb(numb);
        orderVo.setSerialNum(uuid);//订单流水号
        orderVo.setUserId(userEntity.getUserId());
        orderVo.setCityProv(city);
        orderVo.setConsignee(receiver);
        orderVo.setConsigneeMobile(mobile);
        orderVo.setAddress(address);
        return orderVo;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    //确认订单页，暂时不用
    @Override
    public OrderVo confirmOrderGrade(String account, Integer gradeId, String receiver, String mobile, String city, String address) {
        GradeEntity gradeEntity = new GradeEntity();
        gradeEntity.setGradeId(gradeId);
        gradeEntity = gradeMapper.selectOne(gradeEntity);

        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(account);
        userEntity = userMapper.selectOne(userEntity);

        ReceivAddressEntity receivAddressEntity = new ReceivAddressEntity();
        receivAddressEntity.setUserId(userEntity.getUserId());
        receivAddressEntity = receivAddressMapper.selectOne(receivAddressEntity);

        String uuid = UUID.randomUUID().toString().replace("-","");

        //保存订单记录
        OrderEntity entity = new OrderEntity();
        if (receivAddressEntity != null){
            entity.setAddress(receivAddressEntity.getAddress());
            entity.setConsignee(receivAddressEntity.getConsignee());
            entity.setConsigneeMobile(receivAddressEntity.getConsigneeMobile());
        }else{
            receivAddressEntity.setCityProv(city);
            receivAddressEntity.setAddress(address);
            receivAddressEntity.setConsigneeMobile(mobile);
            receivAddressEntity.setConsignee(receiver);
            receivAddressMapper.insert(receivAddressEntity);
        }
        entity.setGType(2);
        entity.setStatus(0);//默认为未支付
        entity.setGid(gradeEntity.getGid());
        entity.setGoodsName(gradeEntity.getGradeName());
        entity.setDisPrice(gradeEntity.getDisPrice());
        entity.setMainPicture(gradeEntity.getMainPicture());
        entity.setSerialNum(uuid);//订单流水号
        entity.setPrice(gradeEntity.getPrice());
        entity.setGivePs(gradeEntity.getGivePs());
        entity.setTotalPrice(gradeEntity.getPrice());
        entity.setUserId(userEntity.getUserId());
        entity.setToGrade(gradeEntity.getToGrade());
        entity.setFromGrade(gradeEntity.getFromGrade());
        entity.setTeam(gradeEntity.getTeam());
        entity.setReConsumption(gradeEntity.getReConsumption());
        entity.setRecommend(gradeEntity.getRecommend());
        entity.setCreateDate(new Timestamp(new Date().getTime()));
        entity.setModifyDate(new Timestamp(new Date().getTime()));
        entity.setAddress(address);
        entity.setConsignee(receiver);
        entity.setConsigneeMobile(mobile);
        entity.setCityProv(city);
        orderMapper.insert(entity);

        //返回对象
        OrderVo orderVo = new OrderVo();
        if (receivAddressEntity != null){
            orderVo.setAddress(receivAddressEntity.getAddress());
            orderVo.setConsignee(receivAddressEntity.getConsignee());
            orderVo.setConsigneeMobile(receivAddressEntity.getConsigneeMobile());
        }
        orderVo.setStatus(0);//默认为未支付
        orderVo.setAddress(receivAddressEntity.getAddress());
        orderVo.setConsignee(receivAddressEntity.getConsignee());
        orderVo.setConsigneeMobile(receivAddressEntity.getConsigneeMobile());
        orderVo.setGid(gradeEntity.getGid());
//        orderVo.setGivePs(gradeEntity.getGivePs());
//        orderVo.setDisPrice(gradeEntity.getDisPrice());
//        orderVo.setPrice(gradeEntity.getPrice());
//        orderVo.setTotalPrice(gradeEntity.getPrice());
        orderVo.setGoodsName(gradeEntity.getGradeName());
        orderVo.setMainPicture(gradeEntity.getMainPicture());
        orderVo.setSerialNum(uuid);
        orderVo.setUserId(userEntity.getUserId());
        orderVo.setCityProv(city);
        orderVo.setConsignee(receiver);
        orderVo.setConsigneeMobile(mobile);
        orderVo.setAddress(address);
        return orderVo;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public List<GoodsInfoVo> getInfoById(Integer goodsId) {
        GoodsInfoEntity goodsInfoEntity = new GoodsInfoEntity();
        goodsInfoEntity.setGoodsId(goodsId);

        List<GoodsInfoEntity> list = goodsInfoMapper.select(goodsInfoEntity);

        List<GoodsInfoVo> goodsInfoVos = new ArrayList<>();
        for (GoodsInfoEntity entity : list) {
            GoodsInfoVo goodsInfoVo = new GoodsInfoVo();
            goodsInfoVo.setGoodsId(entity.getGoodsId());
            goodsInfoVo.setId(entity.getId());
            goodsInfoVo.setPicSort(entity.getPicSort());
            goodsInfoVo.setPicUrl(entity.getPicUrl());
            goodsInfoVos.add(goodsInfoVo);
        }

        return goodsInfoVos;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public List<GoodsRotaionVo> getRotaionById(Integer goodsId) {
        GoodsRotationEntity obj = new GoodsRotationEntity();
        obj.setGoodsId(goodsId);

        List<GoodsRotationEntity> list = goodsRotationMapper.select(obj);

        List<GoodsRotaionVo> vos = new ArrayList<>();
        for (GoodsRotationEntity entity : list) {
            GoodsRotaionVo vo = new GoodsRotaionVo();
            vo.setGoodsId(entity.getGoodsId());
            vo.setId(entity.getId());
            vo.setPicSort(entity.getPicSort());
            vo.setPicUrl(entity.getPicUrl());
            vos.add(vo);
        }

        return vos;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public List<TransactionVo> tranRecord(String account, Integer type, Integer pageSize, Integer rowSize) {

        UserEntity userEntity = new UserEntity();
        userEntity.setAccount(account);
        userEntity = userMapper.selectOne(userEntity);

        TransactionEntity transactionEntity = new TransactionEntity();
        transactionEntity.setUserId(userEntity.getUserId());
        if (!type.equals(0)){
            transactionEntity.setRecordType(type);
        }
        RowBounds rowBounds = new RowBounds((pageSize - 1) * rowSize, rowSize);
        List<TransactionEntity> list = transactionssMapper.selectByRowBounds(transactionEntity,rowBounds);

        List<TransactionVo> vos = new ArrayList<>();
        for (TransactionEntity entity : list) {
            TransactionVo transaction = new TransactionVo();
            transaction.setModifyDate(entity.getModifyDate());
            transaction.setCreateDate(entity.getCreateDate());
            transaction.setRemark(entity.getRemark());
            transaction.setTransacationType(entity.getTType());
            transaction.setRecordType(entity.getRecordType());
            transaction.setAccount(entity.getAccount());
            transaction.setInoutType(entity.getInoutType());
            transaction.setUserId(entity.getUserId());
            BigDecimal nubm = new BigDecimal(entity.getNumb());
            BigDecimal oneh = new BigDecimal(100);
            transaction.setNumb(String.valueOf(nubm.divide(oneh,2, RoundingMode.HALF_UP)));
            vos.add(transaction);
        }

        return vos;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public List<AdminGoodsVo> selectByRowBounds(Integer pageSize, Integer rows) {

        RowBounds rowBounds = new RowBounds((pageSize-1)*rows,rows);
        List<GoodsEntity> list = goodsMapper.selectByRowBounds(new GoodsEntity(),rowBounds);

        List<AdminGoodsVo> listvo = new ArrayList<>();
        for (GoodsEntity entity : list){

            BigDecimal disPrice = new BigDecimal(entity.getDisPrice().toString());
            BigDecimal price = new BigDecimal(entity.getPrice().toString());
            BigDecimal rewardPs = new BigDecimal(entity.getRewardPs().toString());
            BigDecimal oneh = new BigDecimal(100);


            AdminGoodsVo adminGoodsVo = new AdminGoodsVo();
            adminGoodsVo.setContent(entity.getContent());
            adminGoodsVo.setDisPrice(String.valueOf(disPrice.divide(oneh, 2, RoundingMode.HALF_UP)));
            adminGoodsVo.setGid(entity.getGid());
            adminGoodsVo.setGoodsId(entity.getGoodsId());
            adminGoodsVo.setGoodsName(entity.getGoodsName());
            adminGoodsVo.setMainPicture(entity.getMainPicture());
            adminGoodsVo.setPrice(String.valueOf(price.divide(oneh, 2, RoundingMode.HALF_UP)));
            adminGoodsVo.setRemark(entity.getRemark());
            adminGoodsVo.setReturnDays(entity.getReturnDays());
            adminGoodsVo.setCreateDate(entity.getCreateDate());
            adminGoodsVo.setModifyDate(entity.getModifyDate());
            adminGoodsVo.setRewardPs(String.valueOf(rewardPs.divide(oneh, 2, RoundingMode.HALF_UP)));
            listvo.add(adminGoodsVo);
        }

        return listvo;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void updateGoods(Integer goodsId, String goodsName, Integer price, Integer disPrice, Integer rewardPs, Integer returnDays, String mainPicture,String mainPicture1,String mainPicture2, String content) {

        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity.setGoodsId(goodsId);
        goodsEntity = goodsMapper.selectOne(goodsEntity);
        goodsEntity.setGoodsName(goodsName);
        goodsEntity.setPrice(price);
        goodsEntity.setDisPrice(disPrice);
        goodsEntity.setRewardPs(rewardPs);
        goodsEntity.setReturnDays(returnDays);
        goodsEntity.setMainPicture(mainPicture);
        goodsEntity.setContent(content);
        goodsEntity.setModifyDate(new Timestamp(new Date().getTime()));
        goodsMapper.updateByPrimaryKey(goodsEntity);

        //更新商品轮播图
        GoodsRotationEntity goodsRotationEntity = new GoodsRotationEntity();
        goodsRotationEntity.setGoodsId(goodsId);
        goodsRotationEntity.setGType(2);
        List<GoodsRotationEntity> list = goodsRotationMapper.select(goodsRotationEntity);

        GoodsRotationEntity goodsRotationEntity1 = list.get(0);
        goodsRotationEntity1.setPicUrl(mainPicture);
        goodsRotationMapper.updateByPrimaryKey(goodsRotationEntity1);

        if (StringUtils.isNotBlank(mainPicture1)){
            GoodsRotationEntity goodsRotationEntity2 = list.get(1);
            goodsRotationEntity2.setPicUrl(mainPicture1);
            goodsRotationMapper.updateByPrimaryKey(goodsRotationEntity2);
        }
        if (StringUtils.isNotBlank(mainPicture2)){
            GoodsRotationEntity goodsRotationEntity3 = list.get(2);
            goodsRotationEntity3.setPicUrl(mainPicture2);
            list.add(goodsRotationEntity3);
            goodsRotationMapper.updateByPrimaryKey(goodsRotationEntity3);
        }
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public List<GoodsRotaionVo> getIndexRotaion() {
        GoodsRotationEntity obj = new GoodsRotationEntity();
        obj.setGType(3);

        List<GoodsRotationEntity> list = goodsRotationMapper.select(obj);

        List<GoodsRotaionVo> vos = new ArrayList<>();
        for (GoodsRotationEntity entity : list) {
            GoodsRotaionVo vo = new GoodsRotaionVo();
            vo.setGoodsId(entity.getGoodsId());
            vo.setId(entity.getId());
            vo.setPicSort(entity.getPicSort());
            vo.setPicUrl(entity.getPicUrl());
            vos.add(vo);
        }

        return vos;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public List<AdminGradeVo> selectByRowBoundsOfGrade(Integer pageSize, Integer rows) {
        RowBounds rowBounds = new RowBounds((pageSize-1)*rows,rows);
        List<GradeEntity> list = gradeMapper.selectByRowBounds(new GradeEntity(),rowBounds);

        List<AdminGradeVo> listvo = new ArrayList<>();
        for (GradeEntity entity : list){

            BigDecimal disPrice = new BigDecimal(entity.getDisPrice().toString());
            BigDecimal price = new BigDecimal(entity.getPrice().toString());
            BigDecimal rewardPs = new BigDecimal(entity.getGivePs().toString());
            BigDecimal oneh = new BigDecimal(100);


            AdminGradeVo adminGradeVo = new AdminGradeVo();
            adminGradeVo.setContent(entity.getContent());
            adminGradeVo.setDisPrice(String.valueOf(disPrice.divide(oneh, 2, RoundingMode.HALF_UP)));
            adminGradeVo.setGid(entity.getGid());
            adminGradeVo.setGradeId(entity.getGradeId());
            adminGradeVo.setGradeName(entity.getGradeName());
            adminGradeVo.setMainPicture(entity.getMainPicture());
            adminGradeVo.setPrice(String.valueOf(price.divide(oneh, 2, RoundingMode.HALF_UP)));
            adminGradeVo.setRemark(entity.getRemark());
            adminGradeVo.setCreateDate(entity.getCreateDate());
            adminGradeVo.setModifyDate(entity.getModifyDate());
            adminGradeVo.setToGrade(entity.getToGrade());
            adminGradeVo.setTeam(entity.getTeam());
            adminGradeVo.setReConsumption(entity.getReConsumption());
            adminGradeVo.setRecommend(entity.getRecommend());
            adminGradeVo.setFromGrade(entity.getFromGrade());
            adminGradeVo.setGivePs(String.valueOf(rewardPs.divide(oneh, 2, RoundingMode.HALF_UP)));
            listvo.add(adminGradeVo);
        }

        return listvo;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void updateGrade(Integer gradeId, String gradeName, Integer price, Integer disPrice, Integer givePs, Integer reConsumption, Integer team, Integer fromGrade, Integer toGrade, Integer recommend, String mainPicture, String mainPicture1,String mainPicture2,String content) {

        GradeEntity gradeEntity = new GradeEntity();
        gradeEntity.setGradeId(gradeId);
        gradeEntity = gradeMapper.selectOne(gradeEntity);

        gradeEntity.setGradeName(gradeName);
        gradeEntity.setPrice(price);
        gradeEntity.setDisPrice(disPrice);
        gradeEntity.setGivePs(givePs);
        gradeEntity.setRecommend(recommend);
        gradeEntity.setReConsumption(reConsumption);
        gradeEntity.setFromGrade(fromGrade);
        gradeEntity.setToGrade(toGrade);
        gradeEntity.setTeam(team);
        gradeEntity.setMainPicture(mainPicture);
        gradeEntity.setContent(content);
        gradeEntity.setModifyDate(new Timestamp(new Date().getTime()));
        gradeMapper.updateByPrimaryKey(gradeEntity);

        GoodsRotationEntity goodsRotationEntity = new GoodsRotationEntity();
        goodsRotationEntity.setGoodsId(gradeId);
        goodsRotationEntity.setGType(1);
        List<GoodsRotationEntity> list = goodsRotationMapper.select(goodsRotationEntity);

        GoodsRotationEntity goodsRotationEntity1 = list.get(0);
        goodsRotationEntity1.setPicUrl(mainPicture);
        goodsRotationMapper.updateByPrimaryKey(goodsRotationEntity1);
        if (StringUtils.isNotBlank(mainPicture1)){
            GoodsRotationEntity goodsRotationEntity2 = list.get(1);
            goodsRotationEntity2.setPicUrl(mainPicture1);
            goodsRotationMapper.updateByPrimaryKey(goodsRotationEntity2);
        }
        if (StringUtils.isNotBlank(mainPicture2)){
            GoodsRotationEntity goodsRotationEntity3 = list.get(2);
            goodsRotationEntity3.setPicUrl(mainPicture2);
            goodsRotationMapper.updateByPrimaryKey(goodsRotationEntity3);
        }

    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void insertGrade(String gradeName, Integer price, Integer disPrice, Integer givePs, Integer reConsumption, Integer team, Integer fromGrade, Integer toGrade, Integer recommend, String mainPicture,  String mainPicture1, String mainPicture2,String content) {
        GradeEntity gradeEntity = new GradeEntity();

        gradeEntity.setGradeName(gradeName);
        gradeEntity.setPrice(price);
        gradeEntity.setDisPrice(disPrice);
        gradeEntity.setGivePs(givePs);
        gradeEntity.setRecommend(recommend);
        gradeEntity.setReConsumption(reConsumption);
        gradeEntity.setFromGrade(fromGrade);
        gradeEntity.setToGrade(toGrade);
        gradeEntity.setTeam(team);
        gradeEntity.setMainPicture(mainPicture);
        gradeEntity.setContent(content);
        gradeEntity.setGid(0);
        gradeEntity.setRemark("-");
        gradeEntity.setCreateDate(new Timestamp(new Date().getTime()));
        gradeEntity.setModifyDate(new Timestamp(new Date().getTime()));
        gradeMapper.insert(gradeEntity);

        List<GoodsRotationEntity> list = new ArrayList<>();
        GoodsRotationEntity goodsRotationEntity1 = new GoodsRotationEntity();
        goodsRotationEntity1.setGoodsId(gradeEntity.getGradeId());
        goodsRotationEntity1.setPicUrl(mainPicture);
        goodsRotationEntity1.setPicSort(1);
        goodsRotationEntity1.setGType(1);
        list.add(goodsRotationEntity1);
        GoodsRotationEntity goodsRotationEntity2 = new GoodsRotationEntity();
        goodsRotationEntity2.setGoodsId(gradeEntity.getGradeId());
        goodsRotationEntity2.setPicUrl(mainPicture1);
        goodsRotationEntity2.setPicSort(2);
        goodsRotationEntity2.setGType(1);
        list.add(goodsRotationEntity2);
        GoodsRotationEntity goodsRotationEntity3 = new GoodsRotationEntity();
        goodsRotationEntity3.setGoodsId(gradeEntity.getGradeId());
        goodsRotationEntity3.setPicUrl(mainPicture2);
        goodsRotationEntity3.setPicSort(3);
        goodsRotationEntity3.setGType(1);
        list.add(goodsRotationEntity3);
        goodsRotationMapper.insertList(list);
    }

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void insertGoods(String goodsName, Integer price, Integer disPrice, Integer rewardPs, Integer returnDays, String mainPicture, String mainPicture1, String mainPicture2,String content) {
        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity.setGoodsName(goodsName);
        goodsEntity.setPrice(price);
        goodsEntity.setDisPrice(disPrice);
        goodsEntity.setRewardPs(rewardPs);
        goodsEntity.setReturnDays(returnDays);
        goodsEntity.setMainPicture(mainPicture);
        goodsEntity.setContent(content);
        goodsEntity.setGid(0);
        goodsEntity.setRemark("-");
        goodsEntity.setModifyDate(new Timestamp(new Date().getTime()));
        goodsEntity.setCreateDate(new Timestamp(new Date().getTime()));
        goodsMapper.insert(goodsEntity);

        List<GoodsRotationEntity> list = new ArrayList<>();
        GoodsRotationEntity goodsRotationEntity1 = new GoodsRotationEntity();
        goodsRotationEntity1.setGoodsId(goodsEntity.getGoodsId());
        goodsRotationEntity1.setPicUrl(mainPicture);
        goodsRotationEntity1.setPicSort(1);
        goodsRotationEntity1.setGType(2);
        list.add(goodsRotationEntity1);
        GoodsRotationEntity goodsRotationEntity2 = new GoodsRotationEntity();
        goodsRotationEntity2.setGoodsId(goodsEntity.getGoodsId());
        goodsRotationEntity2.setPicUrl(mainPicture1);
        goodsRotationEntity2.setPicSort(2);
        goodsRotationEntity2.setGType(2);
        list.add(goodsRotationEntity2);
        GoodsRotationEntity goodsRotationEntity3 = new GoodsRotationEntity();
        goodsRotationEntity3.setGoodsId(goodsEntity.getGoodsId());
        goodsRotationEntity3.setPicUrl(mainPicture2);
        goodsRotationEntity3.setPicSort(3);
        goodsRotationEntity3.setGType(2);
        list.add(goodsRotationEntity3);
        goodsRotationMapper.insertList(list);

    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void delGoods(Integer goodsId) {
        goodsMapper.deleteByPrimaryKey(goodsId);
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void delGrade(Integer gradeId) {
        gradeMapper.deleteByPrimaryKey(gradeId);
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void OrderReceiver(Integer orderId) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderId(orderId);
        orderEntity = orderMapper.selectOne(orderEntity);
        orderEntity.setStatus(3);
        orderMapper.updateByPrimaryKey(orderEntity);
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public int getGrageCount() {
        return gradeMapper.selectAll().size();
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public int getGoodsCount() {
        return goodsMapper.selectAll().size();
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void updateRoation(String filePath) {
        GoodsRotationEntity goodsRotationEntity = new GoodsRotationEntity();
        goodsRotationEntity.setPicSort(1);
        goodsRotationEntity.setGType(3);
        goodsRotationEntity.setPicUrl(filePath);
        goodsRotationMapper.insert(goodsRotationEntity);
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void delRoation() {
        GoodsRotationEntity goodsRotationEntity = new GoodsRotationEntity();
        goodsRotationEntity.setGType(3);
        goodsRotationMapper.delete(goodsRotationEntity);
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public List<GoodsRotationEntity> rotationList() {
        GoodsRotationEntity goodsRotationEntity = new GoodsRotationEntity();
        goodsRotationEntity.setGType(3);
        return goodsRotationMapper.select(goodsRotationEntity);
    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public AdminCensusVo getCensus() {
        CensusEntity censusEntity = censusMapper.selectAll().get(0);

        BigDecimal CountGivePs = new BigDecimal(censusEntity.getCountGivePs().toString());
        BigDecimal countGoodsBuy = new BigDecimal( censusEntity.getCountGoodsBuy().toString());
        BigDecimal countGradeBuy = new BigDecimal( censusEntity.getCountGradeBuy().toString());
        BigDecimal countRewardArrive = new BigDecimal( censusEntity.getCountRewardArrive().toString());
        BigDecimal countRewardPs = new BigDecimal( censusEntity.getCountRewardPs().toString());
        BigDecimal oneh = new BigDecimal(100);


        AdminCensusVo adminCensusVo = new AdminCensusVo();
        adminCensusVo.setCountGivePs(String.valueOf(CountGivePs.divide(oneh, 2, RoundingMode.HALF_UP)));
        adminCensusVo.setCountGoodsBuy(String.valueOf(countGoodsBuy.divide(oneh, 2, RoundingMode.HALF_UP)));
        adminCensusVo.setCountGoodsOrder(String.valueOf(censusEntity.getCountGoodsOrder()));
        adminCensusVo.setCountGradeBuy(String.valueOf(countGradeBuy.divide(oneh, 2, RoundingMode.HALF_UP)));
        adminCensusVo.setCountGradeOrder(String.valueOf(censusEntity.getCountGradeOrder()));
        adminCensusVo.setCountRewardArrive(String.valueOf(countRewardArrive.divide(oneh, 2, RoundingMode.HALF_UP)));
        adminCensusVo.setCountRewardPs(String.valueOf(countRewardPs.divide(oneh, 2, RoundingMode.HALF_UP)));
        adminCensusVo.setCountUser(String.valueOf(censusEntity.getCountUser()));
        return adminCensusVo;

    }
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void updateOrder(Integer orderId, String address, String consignee, String consigneeMobile) {

        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderId(orderId);
        orderEntity = orderMapper.selectOne(orderEntity);

        if (orderEntity.getStatus().equals(4)){
            throw new ApiException(ErrorCodeEnum.PARAM_200033);
        }
        orderEntity.setAddress(address);
        orderEntity.setConsigneeMobile(consigneeMobile);
        orderEntity.setConsignee(consignee);
        orderMapper.updateByPrimaryKey(orderEntity);
    }

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void cancelOrder(Integer orderId) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderId(orderId);
        orderEntity = orderMapper.selectOne(orderEntity);

        if (orderEntity.getGType().equals(2)){
            throw new ApiException(ErrorCodeEnum.PARAM_200031);
        }
        if (orderEntity.getStatus().equals(3)){
            throw new ApiException(ErrorCodeEnum.PARAM_200032);
        }
        if (orderEntity.getStatus().equals(4)){
            throw new ApiException(ErrorCodeEnum.PARAM_200033);
        }
        orderEntity.setStatus(4);
        orderMapper.updateByPrimaryKey(orderEntity);

        //用户账户积分返还
        UserPackEntity userPackEntity = new UserPackEntity();
        userPackEntity.setUserId(orderEntity.getUserId());
        userPackEntity = userPackMapper.selectOne(userPackEntity);

        OperatorLogsEntity operatorLogsEntity = new OperatorLogsEntity();
        operatorLogsEntity.setOrderId(orderId);
        List<OperatorLogsEntity> operatorLogsEntityList = operatorLogsMapper.select(operatorLogsEntity);
        for (OperatorLogsEntity operatorLogsEntity1 : operatorLogsEntityList){
            //添加明细记录 用户取消商城订单返回积分记录
            TransactionEntity out = new TransactionEntity();
            out.setNumb(operatorLogsEntity1.getNumb());
            out.setRemark("商城订单取消返回PS");
            out.setTType(16);
            if (operatorLogsEntity1.getPackType().equals(0)){
                userPackEntity.setFreeIntegral(userPackEntity.getFreeIntegral() + operatorLogsEntity1.getNumb());
                out.setRemark("商城订单取消返回PS");
                out.setTType(16);
            }else{
                userPackEntity.setRewardIntegral(userPackEntity.getRewardIntegral() + operatorLogsEntity1.getNumb());
                out.setRemark("商城订单取消返回消费积分");
                out.setTType(17);
            }
            out.setCreateDate(new Timestamp(new Date().getTime()));
            out.setModifyDate(new Timestamp(new Date().getTime()));
            out.setRecordType(0);
            out.setInoutType(1);
            out.setUserId(orderEntity.getUserId());
            out.setAccount(orderEntity.getAccount());
            transactionssMapper.insert(out);
        }
        userPackMapper.updateByPrimaryKey(userPackEntity);

    }
}
